import { getDiary, writeDiary, update } from './service';
import { message } from 'antd';
const creator = (type, payload) => ({ type, payload });
export default {
  namespace: 'diary',
  state: {
    diaries: [],
  },
  effects: {
    //获取日记列表
    *getDiary({ payload }, { call, put }) {
      const res = yield call(getDiary, payload);
      const { date } = payload;
      if (res.httpStatus === 200) {
        //date为null表示获取所有
        if (date==='all') {
          yield put(creator('saveDiaries', res.data));
        }
      }
      return res;
    },
    *writeDiary({ payload }, { call, put }) {
      const res = yield call(writeDiary, payload);
      return res;
    },
    *update({ payload }, { call, put }) {
      const res = yield call(update, payload);
      return res;
    },
  },
  reducers: {
    saveDiaries(state, { payload }) {
      return {
        ...state,
        diaries: payload,
      };
    },
  },
};
